package com.yaoyanshe.trialfield.common.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

/**
 * https://muyangmin.github.io/glide-docs-cn/doc/transformations.html
 * author: tck
 * created on: 2018/6/25 13:33
 * description:
 */
public class ImageLoaderUtil {

    private ImageLoaderUtil() {

    }

    public static void loadImage(Context context,
                                 ImageView image,
                                 String url,
                                 @DrawableRes int defaultImage) {
        if (context != null) {
            Glide.with(context)
                    .load(url)
                    .apply(
                            RequestOptions.centerCropTransform()
                                    .error(defaultImage)
                                    .fallback(defaultImage)
                                    .placeholder(defaultImage)
                    )
                    .into(image);
        }
    }


    public static void loadImageNormal(Context context,
                                 ImageView image,
                                 String url,
                                 @DrawableRes int defaultImage) {
        if (context != null) {
            Glide.with(context)
                    .load(url)
                    .error(defaultImage)
                    .fallback(defaultImage)
                    .placeholder(defaultImage)
                    .into(image);
        }
    }

    /**
     * .skipMemoryCache(true)
     * .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
     *
     * @param context
     * @param image
     * @param url
     * @param radius
     * @param defaultImage
     */
    public static void loadRoundImage(Context context,
                                      ImageView image,
                                      String url,
                                      int radius,
                                      @DrawableRes int defaultImage) {
        if (context != null) {
            Glide.with(context)
                    .load(url)
                    .apply(
                            new RequestOptions()
                                    .transform(new CenterCrop(), new RoundedCorners(radius))
                                    .error(defaultImage)
                                    .fallback(defaultImage)
                                    .placeholder(defaultImage)

                    )
                    .into(image);
        }
    }

    /**
     * .skipMemoryCache(true)
     * .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
     *
     * @param context
     * @param image
     * @param resourceId
     * @param radius
     * @param defaultImage
     */
    public static void loadRoundImage(Context context,
                                      ImageView image,
                                      @DrawableRes int resourceId,
                                      int radius,
                                      @DrawableRes int defaultImage) {
        if (context != null) {
            Glide.with(context)
                    .load(resourceId)
                    .apply(
                            new RequestOptions()
                                    .transform(new CenterCrop(), new RoundedCorners(radius))
                                    .error(defaultImage)
                                    .fallback(defaultImage)
                                    .placeholder(defaultImage)

                    )
                    .into(image);
        }
    }

    public static void loadCircleImage(Context context,
                                       ImageView image,
                                       String url,
                                       @DrawableRes int defaultImage) {
        if (context != null) {
            Glide.with(context)
                    .load(url)
                    .apply(
                            RequestOptions.circleCropTransform()
                                    .error(defaultImage)
                                    .placeholder(defaultImage)
                                    .fallback(defaultImage)
                    )
                    .into(image);
        }
    }

    public static String getImageUrl(String url, int width, int height) {
        if (TextUtils.isEmpty(url)) {
            return "";
        }

        if (url.contains("?imageView2")) {
            return url;
        }

        StringBuilder sb = new StringBuilder();
        sb.append(url)
                .append("?imageView2/1")
                .append("/w/")
                .append(width)
                .append("/h/")
                .append(height);
        return sb.toString();
    }

    public static String getImageUrl2(String url, int width) {
        if (!TextUtils.isEmpty(url) && url.contains("?imageView2/2/w")) {

            return url;
        }

        StringBuilder sb = new StringBuilder();
        sb.append(url)
                .append("?imageView2/2/w")
                .append(width);
        return sb.toString();
    }


}
